﻿using System;
using System.Data;
using System.Configuration;
using System.Web;


using System.Net.Mail;
using System.Net.Sockets;
namespace LearnNet.Common
{
    /// <summary>
    ///Email 的摘要说明
    /// </summary>
    public class Email
    {
        public Email()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 单封邮件发送
        /// </summary>
        /// <param name="strTo">收件人地址(可以是多个地址用"|"/","/";"隔开)</param>
        /// <param name="strTitle">邮件标题</param>
        /// <param name="strBody">邮件内容</param>
        /// <param name="strIsHTML">是否是HTML邮件</param>
        /// <param name="strFrom">发件人地址</param>
        /// <param name="strLoginName">发件人名称</param>
        /// <param name="strPassword">发件人邮箱密码</param>
        /// <param name="strSMTP">发件人SMTP</param>
        /// <returns></returns>
        public  string SendMail(string strTo, string strTitle, string strBody, bool strIsHTML, string strFrom, string strLoginName, string strPassword, string strSMTP)
        {
            System.Threading.Thread.Sleep(500);
            string[] str = strTo.Split(new char[] { ',', ';', '|' });
            string strCount = "";
            LearnNet.Common.RegExpression.IRegExpression regExpre = new LearnNet.Common.RegExpression.EmailRegExpression();
            
            for (int i = 0; i < str.Length; i++)
            {

                if (!regExpre.IsMatch(str[i]))
                {
                    strCount = "收件人邮箱格式不正确";
                    return strCount;
                }
                if (!regExpre.IsMatch(strFrom))
                {
                    strCount = "发件人邮箱格式不正确";
                    return strCount;
                }
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(strFrom, strLoginName, System.Text.Encoding.UTF8);
                /* 上面3个参数分别是发件人地址，发件人姓名，编码*/
                msg.Subject = strTitle;                           //邮件标题 
                msg.SubjectEncoding = System.Text.Encoding.UTF8;  //邮件标题编码 
                msg.Body = strBody;                               //邮件内容 
                msg.BodyEncoding = System.Text.Encoding.UTF8;     //邮件内容编码 
                msg.IsBodyHtml = strIsHTML;                       //是否是HTML邮件 
                msg.Priority = MailPriority.High;               //邮件优先级 Normal
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(strFrom, strPassword);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Host = strSMTP;
                object userState = msg;
                msg.To.Add(str[i].ToString());

                try
                {
                    System.Threading.Thread.Sleep(100);
                    client.Send(msg);
                    strCount = "发送成功";
                }
                catch (SmtpException ex)
                {
                    strCount = string.Concat("发送失败：", ex.Message);
                }
            }
            return strCount;
        }
        /// <summary>
        /// 群发邮件
        /// </summary>
        /// <param name="strTo">收件人地址(可以是多个地址用"|"/","/";"隔开)</param>
        /// <param name="strTitle">邮件标题</param>
        /// <param name="strBody">邮件内容</param>
        /// <param name="strIsHTML">是否是HTML邮件</param>
        /// <param name="strFrom">发件人地址</param>
        /// <param name="strLoginName">发件人名称</param>
        /// <param name="strPassword">发件人邮箱密码</param>
        /// <param name="strSMTP">发件人SMTP</param>
        /// <returns></returns>
        public  string SendMail(string strTo, string strTitle, string strBody, bool strIsHTML, string strFrom, string strLoginName, string strPassword, string strSMTP, string strFree)
        {
            string[] str = strTo.Split(new char[] { ',', ';', '|' });
            string strCount = "";
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(strFrom, strLoginName, System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址，发件人姓名，编码*/
            msg.Subject = strTitle;                           //邮件标题 
            msg.SubjectEncoding = System.Text.Encoding.UTF8;  //邮件标题编码 
            msg.Body = strBody;                               //邮件内容 
            msg.BodyEncoding = System.Text.Encoding.UTF8;     //邮件内容编码 
            msg.IsBodyHtml = strIsHTML;                       //是否是HTML邮件 
            msg.Priority = MailPriority.High;               //邮件优先级 
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(strFrom, strPassword);
            client.Host = strSMTP;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            object userState = msg;
            LearnNet.Common.RegExpression.IRegExpression regExpre = new LearnNet.Common.RegExpression.EmailRegExpression();
            for (int i = 0; i < str.Length; i++)
            {
                if (!regExpre.IsMatch(str[i]))
                {
                    strCount = "收件人邮箱格式不正确";
                    return strCount;
                }
                if (!regExpre.IsMatch(strFrom))
                {
                    strCount = "发件人邮箱格式不正确";
                    return strCount;
                }
                msg.To.Add(str[i].ToString());
            }
            try
            {
                System.Threading.Thread.Sleep(200);
                client.Send(msg);
                strCount = "发送成功";
            }
            catch (SmtpException ex)
            {
                strCount = string.Concat( "发送失败",ex.Message);
            }
            return strCount;
        }
    }


}

